package com.song.EMS.controller;

import com.song.EMS.bean.Express;
import com.song.EMS.bean.Message;
import com.song.EMS.bean.ResultData;
import com.song.EMS.mvc.ResponseBody;
import com.song.EMS.service.impl.ExpressServiceImpl;
import com.song.EMS.utils.GetAdminPhone;
import com.song.EMS.utils.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ExpressController {
    private ExpressServiceImpl expressService = new ExpressServiceImpl();

    /**
     * 查询所有快递信息
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/express/select.do")
    public String selectAll(HttpServletRequest request, HttpServletResponse response) {
        //获取前端数据
        String pageIndex = request.getParameter("pageIndex");
        String pageSize = request.getParameter("pageSize");
        //调用service
        List<Express> expresses = expressService.selectExpress(true, Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        //信息总数
        int total = expressService.selectExpress(false, 0, 0).size();
        ResultData<Express> data = new ResultData<>();
        data.setRows(expresses);
        data.setTotal(total);
        return JsonUtil.toJSON(data);
    }

    /**
     * 新添快递
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/express/insert.do")
    public String insertExpress(HttpServletRequest request, HttpServletResponse response) {
        Message data = null;
        //获取前端数据
        String expressNumber = request.getParameter("expressNumber");
        String company = request.getParameter("company");
        String receiverName = request.getParameter("receiverName");
        String receiverPhone = request.getParameter("receiverPhone");

        //获取录入人号码
        String writerPhone = GetAdminPhone.getPhone(request);
        Express express = new Express(expressNumber, company, receiverName, receiverPhone, writerPhone);
        Integer integer = expressService.insertExpress(express);
        if (integer == 1) {
            data = new Message(1, "添加成功");
        } else if (integer == -1) {
            data = new Message(-1, "快递单号已存在");
        } else {
            data = new Message(0, "添加失败");
        }
        return JsonUtil.toJSON(data);
    }

    /**
     * 通过快递单号查询快递
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/express/selectByNumber.do")
    public String selectByNumber(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        String expressNumber = request.getParameter("expressNumber");
        System.out.println(expressNumber);
        //调用service
        Express express = expressService.selectByNumber(expressNumber);
        if (express == null){
            data = new Message(0,"改单号不存在");

        }else {
            data = new Message(1,"查询成功",express);
        }
        System.out.println(data.getResult());
        return JsonUtil.toJSON(data);
    }

    /**
     * 修改快递信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/express/update.do")
    public String updateExpress(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        //获取前端数据
        String expressId = request.getParameter("expressId");
        System.out.println(expressId);
        String expressNumber = request.getParameter("expressNumber");
        String company = request.getParameter("company");
        String receiverName = request.getParameter("receiverName");
        String receiverPhone = request.getParameter("receiverPhone");
        String status = request.getParameter("status");
        System.out.println(status);
        //获取录入人号码
        String writerPhone = GetAdminPhone.getPhone(request);
        //调用service
        Express express = new Express(Integer.parseInt(expressId), expressNumber, company, receiverName, receiverPhone,Integer.parseInt(status),writerPhone);
        Integer integer = expressService.updateExpress(express);
        if (integer == 1){
            data = new Message(1,"修改成功");
        }else if (integer == -1){
            data = new Message(-1,"快递单号已存在");
        }else {
            data = new Message(0, "修改失败");
        }
        System.out.println(data.getStatus());
        System.out.println(data.getResult());
        return JsonUtil.toJSON(data);

    }

    /**
     * 删除快递
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/express/delete.do")
    public String deleteExpress(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        //获取前端数据
        String expressId = request.getParameter("expressId");

        Integer integer = expressService.deleteExpress(Integer.parseInt(expressId));
        if (integer == 1){
            data = new Message(1,"删除成功");
        }else {
            data = new Message(0, "删除失败");
        }
        return JsonUtil.toJSON(data);
    }

    /**
     * 控制台中的数据
     * @param request
     * @param response
     * @return 给前端的用户数据
     */
    @ResponseBody("/admin/views/express-console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        //调用service
        Message data = null;
        List<Integer> expressList = expressService.expressConsole();
        for (Integer integer : expressList) {
            System.out.println(integer);
        }
        if(expressList.size() >0){
            data = new Message(1,null,expressList);

        }else {
            data = new Message(-1,null);
        }

        return JsonUtil.toJSON(data);
    }


}
